package com.he1618;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Cluster;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class NacosApp {

    public static void main(String[] args) throws NacosException, InterruptedException {

        Properties properties = new Properties();
        properties.put("serverAddr", "192.168.3.108:8848");
        properties.put("username","boxCloud");
        properties.put("password","he1618");
        properties.put("namespace","c1a1a27d-d7e7-4d31-be64-c6c985b72ad3");
        properties.put("group","test");


        NamingService naming = NamingFactory.createNamingService(properties);
       // naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");

        Instance instance = new Instance();
        instance.setIp("192.168.3.108");
        instance.setPort(2001);
        instance.setHealthy(true);
        instance.setWeight(1.0);
        instance.setEnabled(true);
        Map<String, String> instanceMeta = new HashMap<>();
        instanceMeta.put("preserved.register.source", "SPRING_CLOUD");
        instance.setMetadata(instanceMeta);





        naming.registerInstance("ims","test", instance);

        naming.subscribe("test", new EventListener() {
            @Override
            public void onEvent(Event event) {
                System.out.println(((NamingEvent)event).getServiceName());
                System.out.println(((NamingEvent)event).getInstances());
            }
        });
        Thread.sleep(1000000);
    }
}
